package com.example.smartwaterapi.controller;

import com.example.smartwaterapi.common.api.RestApiResponse;
import com.example.smartwaterapi.entity.WaterAchievement;
import com.example.smartwaterapi.service.WaterAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 饮水成就控制器
 */
@Slf4j
@Tag(name = "饮水成就管理API", description = "饮水成就相关接口")
@RestController
@RequestMapping("/api/water-achievement")
public class WaterAchievementController {

    @Autowired
    private WaterAchievementService waterAchievementService;

    @Operation(summary = "获取成就详情", description = "根据ID获取成就信息")
    @GetMapping("/{id}")
    public RestApiResponse<WaterAchievement> getWaterAchievement(@Parameter(description = "成就ID") @PathVariable Long id) {
        log.info("获取饮水成就详情, id: {}", id);
        try {
            WaterAchievement achievement = waterAchievementService.getById(id);
            if (achievement == null) {
                log.warn("未找到饮水成就记录, id: {}", id);
                return RestApiResponse.notFound("成就记录不存在");
            }
            log.info("成功获取饮水成就记录, id: {}", id);
            return RestApiResponse.ok(achievement);
        } catch (Exception e) {
            log.error("获取饮水成就详情异常, id: {}", id, e);
            return RestApiResponse.error("获取成就记录失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "创建饮水成就", description = "创建新的饮水成就记录")
    @PostMapping
    public RestApiResponse<WaterAchievement> createWaterAchievement(@RequestBody WaterAchievement achievement) {
        log.info("创建饮水成就记录: {}", achievement);
        try {
            boolean success = waterAchievementService.save(achievement);
            if (success) {
                log.info("饮水成就记录创建成功, id: {}", achievement.getId());
                return RestApiResponse.created(achievement);
            } else {
                log.warn("饮水成就记录创建失败");
                return RestApiResponse.error("创建成就记录失败");
            }
        } catch (Exception e) {
            log.error("创建饮水成就记录异常", e);
            return RestApiResponse.error("创建成就记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "更新饮水成就", description = "更新已有的饮水成就记录")
    @PutMapping("/{id}")
    public RestApiResponse<Boolean> updateWaterAchievement(
            @Parameter(description = "成就ID") @PathVariable Long id,
            @RequestBody WaterAchievement achievement) {
        log.info("更新饮水成就记录, id: {}, data: {}", id, achievement);
        try {
            achievement.setId(id);
            boolean success = waterAchievementService.updateById(achievement);
            if (success) {
                log.info("饮水成就记录更新成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("饮水成就记录更新失败, id: {}", id);
                return RestApiResponse.notFound("更新成就记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("更新饮水成就记录异常, id: {}", id, e);
            return RestApiResponse.error("更新成就记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "删除饮水成就", description = "删除指定的饮水成就记录")
    @DeleteMapping("/{id}")
    public RestApiResponse<Boolean> deleteWaterAchievement(@Parameter(description = "成就ID") @PathVariable Long id) {
        log.info("删除饮水成就记录, id: {}", id);
        try {
            boolean success = waterAchievementService.removeById(id);
            if (success) {
                log.info("饮水成就记录删除成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("饮水成就记录删除失败, id: {}", id);
                return RestApiResponse.notFound("删除成就记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("删除饮水成就记录异常, id: {}", id, e);
            return RestApiResponse.error("删除成就记录异常: " + e.getMessage());
        }
    }
} 